<?php
namespace app\manager\controller;

use think\Controller;
use think\Request;
/**
 * 营地管理
 */
class Camp extends \think\Controller
{
  use \app\common\traits\controller\TraitUpload;
    //protected $check_auths = false; 
    /**
     * GET shwo camp info list
     * @return [type] [description]
     */
    
    public function index()
    {
        $list = model('Camps')->getCampList();//halt($list);
        $this->assign('list',$list);
        return view('p/camp/index');     
    }
    /**
     * GET show camp info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function read($id)
    {
        $campinfo = model('Camps')->getCampInfo($id);
        $this->assign('campinfo',$campinfo);
        $camptypename = model('CampTypes')->where('id',$campinfo['type'])->value('name');
        $this->assign('camptypename',$camptypename);
        $campgradename = model('CampGrades')->where('id',$campinfo['grade'])->value('name');
        $this->assign('campgradename',$campgradename);
        return view('p/camp/read');
    }
    /**
     * GET shwo create camp info
     * @return [type] [description]
     */
    public function create()
    {
        // 类型
        $types = model('CampTypes')->getCampTypeLists();
        $types = array_column($types->toArray(), 'name', 'id');
        $this->assign('types',$types);
        // 星级
        $grades = model('CampGrades')->getCampGradeLists();
        $grades = array_column($grades->toArray(), 'name', 'id');
        $this->assign('grades',$grades);
        
        return view('p/camp/create');
    }
    /**
     * POST create new camp info
     * @return [type] [description]
     */
    public function save()
    {


        $post_camp_info['name'] = input('post.name');
        $post_camp_info['number'] = input('post.number');
        $post_camp_info['type'] = input('post.type');
        $post_camp_info['grade'] = input('post.grade');
        $post_camp_info['count'] = input('post.count');
        $post_camp_info['lat'] = input('post.lat/f');
        $post_camp_info['lng'] = input('post.lng/f');
        $post_camp_info['profiles'] = input('post.profiles');
        $post_camp_info['content'] = input('post.content');
        $post_camp_info['email'] = input('post.email');
        $post_camp_info['travel_money_base'] = input('post.travel_money_base');
        $post_camp_info['legal_person'] = input('post.legal_person');
        $post_camp_info['credit_code'] = input('post.credit_code');
        $post_camp_info['branch'] = input('post.branch');
        $post_camp_info['campmanager_phone'] = input('post.campmanager_phone');
        $post_camp_info['campmanager_name'] = input('post.campmanager_name');
        $post_camp_info['cover'] = input('post.cover');
        $post_camp_info['license'] = input('post.license');
        $post_camp_info['tel'] = input('post.tel');
        $post_camp_info['address'] = [input('post.country'),input('post.state'),input('post.city')];

        //dump($post_camp_info);die;

        $camp = model('Camps')->createCamps($post_camp_info);

        if($camp){
            return $this->success(lang('add camp success'), '/camp');
        }
        return $this->error(lang('add camp failed').$camp['msg']);
    }

    /**
     * GET edit camp info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function edit($id)
    {
        $list=model('Camps')->where('id',$id)->find();
        $this->assign('list',$list);//halt($list);

        // 类型 
        $types = model('CampTypes')->getCampTypeLists();
        $types = array_column($types, 'name', 'id');
        $this->assign('types',$types);
        // 星级
        $grades = model('CampGrades')->getCampGradeLists();
        $grades = array_column($grades, 'name', 'id');
        $this->assign('grades',$grades);
        

        return view('p/camp/edit');
        //return view('camp/edit',['camp'=>$camp['data'], 'types'=>$types, 'grades'=>$grades, 'facilities'=>$facilities, 'services'=>$services]);
    }

    /**
     * GET edit camp info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function check()
    {
        

        return view('p/camp/check');
        //return view('camp/edit',['camp'=>$camp['data'], 'types'=>$types, 'grades'=>$grades, 'facilities'=>$facilities, 'services'=>$services]);
    }

    /**
     * PUT update camp info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function update($id)
    {
        // $post_camp_info['name'] = input('post.name');
        // $post_camp_info['number'] = input('post.number');
        // $post_camp_info['type'] = input('post.type');
        // $post_camp_info['grade'] = input('post.grade');
        // $post_camp_info['count'] = input('post.count');
        //$post_camp_info['address'] = [input('post.country'),input('post.state'),input('post.city')];
        $post_camp_info['lat'] = input('post.lat/f');
        $post_camp_info['lng'] = input('post.lng/f');
        $post_camp_info['profiles'] = input('post.profiles');
        $post_camp_info['content'] = input('post.content');
        $post_camp_info['email'] = input('post.email');
        // $post_camp_info['travel_money_base'] = input('post.travel_money_base');
        // $post_camp_info['legal_person'] = input('post.legal_person');
        // $post_camp_info['credit_code'] = input('post.credit_code');
        // $post_camp_info['branch'] = input('post.branch');
        // $post_camp_info['campmanager_phone'] = input('post.campmanager_phone');
        // $post_camp_info['campmanager_name'] = input('post.campmanager_name');
        // $post_camp_info['cover'] = input('post.cover');
        // $post_camp_info['license'] = input('post.license');
        $post_camp_info['tel'] = input('post.tel');
        if (input('post.caravancover')) {
            $post_camp_info['cover'] = input('post.caravancover');
        }
        if (input('post.license')) {
            $post_camp_info['license'] = input('post.license');
        }
        
        if (input('post.country') && input('post.province') && input('post.city')) {
            $post_camp_info['address'] = input('post.country');
            $post_camp_info['address'] = input('post.province');
            $post_camp_info['address'] = input('post.city');
        }
        $post_camp_info['address'] = input('post.address');
        //halt($post_camp_info);
        $camp = model('Camps')->updateCamp($id, $post_camp_info);

        if ($camp) {

            return $this->success(lang('update camp success'), '/camp');
        } 
        return $this->error(lang('update camp failed').$camp['msg']);        
    }
    /**
     * DELETE camp info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete()
    {
        // $camp = model('Camps')->delCamp($id);
        // if ($camp['code'] === 0) {
        //     return $this->success(lang('delete camp success'), '/camp');
        // }    
        // return $this->error(lang('delete camp failed'));      
        $ids=input('post.ids/a');
        //dump($ids);die;
        if (empty($ids)) {
          return $this->error(lang('delete is null')); 
        }
        if (model('Camps')->delCamp($ids)) {
            return $this->success(lang('delete camp success'), '/camp');
        }    
        return $this->error(lang('delete camp failed'));  
    }
    /**
     * 回收站数据
     * @return [type] [description]
     */
    public function getRecycle()
    {
        $query = $this->setQuerys();
        $camps = model('Camps')->getCampsListsOnlyTrashed($query, $order = 'updated_at desc');
        return view('p/camp/recycle',['camps'=>$camps]);
    }
    /**
     * 草稿箱数据
     * @return [type] [description]
     */
    public function getDraft()
    {

        $query = $this->setQuerys();
        $camps = model('Camps')->getCampsListsDrafts($query, $order = 'updated_at desc');
        return view('p/camp/draft',['camps'=>$camps]);
    }

    /**
     * 价格数据
     * @return [type] [description]
     */
    public function getPrice($id)
    {
        $list=model('Camps')->where('id',$id)->find();
        $this->assign('list',$list);
        return view('p/camp/price');
    }
    /**
     * 恢复删除数据
     * @param  uuid $id 要恢复数据的id
     * @return [type]     [description]
     */
    public function reset()
    {
        // $camp = model('Camps')->campRestore($id);
        // //return;
        // if ($camp['code'] === 0) {
        //     return $this->success(lang('reset camp success'), '/camp');
        // }    
        // return $this->error(lang('reset camp failed'));  

        $ids=input('post.ids/a');
        //dump($ids);die;
        if (empty($ids)) {
          return $this->error(lang('reset is null')); 
        }
        if (model('Camps')->campRestore($ids)) {
            return $this->success(lang('reset camp success'), '/camp');
        }    
        return $this->error(lang('reset camp failed'));  

    }
    public function setStauts()
    {
        $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error(lang('revise is null')); 
        }
        $status=input('post.status/d');
        if (model('Camps')->setInfoStatus($ids, $status)) {
            return $this->success(lang('to  revise  success'), '/camp');
        }
        return $this->error(lang('to  revise  failed'));
    }
    public function forcedel()
     {
       $ids=input('post.ids/a');
       //dump($ids);
        if (empty($ids)) {
          return $this->error(lang('forcedel is null')); 
        }
        //halt(model('Camps')->delCamp($ids,true));
        if (model('Camps')->delCamp($ids,true)) {
            return $this->success(lang('forcedel camp success'), '/camp/del');
        }    
        return $this->error(lang('forcedel camp failed'));   
     }

     public function Map($id)
    {
        //halt($id);
        $camp_list=model('Camps')->where('id',$id)->find();
        return view('p/camp/map',['camp_list'=>$camp_list]);

    }

      public function setMap($id)
    {

        if($f = model('Upload','model\service')->go('img')){
            $map_id= $f['id'];
            if (model('Camps')->setImg($id, $map_id)) {
            return $this->success(lang('to  Upload  success'), '/camp');
             }
            return $this->error(lang('to  Upload  failed'));
        }

    }

    //地区插件
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();;
        // echo   json_encode($country);
        // exit;
        return json($country);
    }
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }

    public function imgUpload()
    {
      if ($this->upload('file')) {
        $url = $this->getUploadInfos();
        return json(array(
                "state" => 'SUCCESS',
                "url" => $url['url'],
            ));
      }
    }








}

